package com.cf.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author ChenFeng
 * @date 2021/11/4 6:50 下午
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private Integer code;

    private String message;

    private T data;

    private Boolean success;

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("成功");
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    public static <T> Result<T> success(T t) {
        Result<T> result = success();
        result.setData(t);
        return result;
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMessage("失败");
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static <T> Result<T> fail(Integer code) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage("失败");
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = fail(code);
        result.setMessage(message);
        return result;
    }

}
